package com.test.base.service;

import com.test.base.dao.NideshopGoodsSpecificationDao;
import com.test.base.dao.NideshopProductDao;
import com.test.base.entity.NideshopGoodsSpecificationExt;
import com.test.base.entity.NideshopProduct;
import com.test.base.utils.Assist;
import com.test.base.vo.service.SpecificationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private NideshopGoodsSpecificationDao nideshopGoodsSpecificationDao;
    @Autowired
    private NideshopProductDao nideshopProductDao;

    /**
     * 获取商品的product
     *
     * @param goodsId 商品ID
     * @return
     */
    public List<NideshopProduct> getProductList(Long goodsId) {
        Assist assist = new Assist();
        assist.setRequires(Assist.andEq("goods_id", goodsId));
        return nideshopProductDao.selectNideshopProduct(assist);
    }

    /**
     * 获取商品的规格信息
     *
     * @param goodsId
     */
    public ArrayList<SpecificationDto> getSpecificationList(Long goodsId) {
        List<NideshopGoodsSpecificationExt> nideshopGoodsSpecificationExts =
                nideshopGoodsSpecificationDao.selectSpecificationByGoodsId(goodsId);
        List<SpecificationDto> specificationList = new ArrayList<>();
        HashMap<Long, SpecificationDto> hasSpecificationList = new HashMap<>();
        for (NideshopGoodsSpecificationExt goodsSpecification : nideshopGoodsSpecificationExts) {
            if (!hasSpecificationList.containsKey(goodsSpecification.getId())) {
                SpecificationDto specificationDto = new SpecificationDto();
                specificationDto.setName(goodsSpecification.getName());
                specificationDto.setSpecificationId(goodsSpecification.getSpecificationId());
                List<NideshopGoodsSpecificationExt> list = new ArrayList<>();
                list.add(goodsSpecification);
                specificationDto.setValueList(list);
                hasSpecificationList.put(goodsSpecification.getId(), specificationDto);
            } else {
                SpecificationDto specificationDto = hasSpecificationList.get(goodsSpecification.getId());
                specificationDto.getValueList().add(goodsSpecification);
            }
        }
        return  new ArrayList<SpecificationDto>(hasSpecificationList.values());
    }

}
